Spring JPA 不使用缓存的方法

最经遇到了一个问题,就是在使用JPA的时候,明明写入数据库,但是却完全查不出来,究其原因,可能就是JPA的缓存机制,当然也可以用别的方法,比如说给DAO一个固定的EntityManager,因为查不出来的原因可能就是因为在查询和插入的时候使用了不同的EntityManager。

在JPA的配置文件persistent.xml中使用persistent_2_0.xsd,即如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
            _ http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"_**>**
然后增加配置:
 <shared-cache-mode>NONE</shared-cache-mode>
shared-cache-mode属性有4种配置:ALL-缓存所有实体;NONE-禁止缓存;ENABLE_SELECTIVE-如果加了缓存的标识,是默认的选项;DISABLE_SELECTIVE- 使用缓存除非使用注解@Cacheable(false),这个不推荐使用 下面是一个persistent.xml的举例,不过不是Spring的,是Hibernate的,而且是从网上摘的,网址为[点我](http://www.cnblogs.com/luxh/archive/2012/05/24/2516282.html)
 <?xml version="1.0" encoding="UTF-8"?>
  <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
              http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

     <!--必须要有name属性,不能为空 -->
      <persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
           <!--可选 -->
           <provider>org.hibernate.ejb.HibernatePersistence</provider>
           <!--可选 -->
           <jta-data-source>java:/DefaultDS</jta-data-source>
           <!--可选 -->
           <mapping-file>ormap.xml</mapping-file>
           <!--可选 -->
           <jar-file>MyApp.jar</jar-file>
           <!--可选 -->
           <class>org.acme.Employee</class>
           <!--可选 -->
           <shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
           <!--可选 -->
           <validation-mode>CALLBACK</validation-mode>

           <!--厂商的特定属性 -->
           <properties>
              <!--配置Hibernate方言 -->
              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
              <!--配置数据库驱动 -->
              <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
              <!--配置数据库用户名 -->
              <property name="hibernate.connection.username" value="root" />
              <!--配置数据库密码 -->
              <property name="hibernate.connection.password" value="root" />
              <!--配置数据库url -->
              <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&amp;characterEncoding=UTF-8" />
              <!--设置外连接抓取树的最大深度 -->
              <property name="hibernate.max_fetch_depth" value="3" />
              <!--自动输出schema创建DDL语句 -->
              <property name="hibernate.hbm2ddl.auto" value="update" />    
           </properties>
      </persistence-unit>

  </persistence>
**xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"** 要注意使用的是2.0规范 **name** JPA2.0规范要求每一个持久化单元必须有一个名字,不能为空。即persistence-unit name="manager1"的name不能为空。 **transaction-type** 使用的事务类型。有JTA和RESOURCE_LOCAL两种类型可以选择。在JavaEE环境中默认为JTA,在JavaSE环境中默认为RESOURCE_LOCAL。当在persistent.xml文件使用,默认就是JTA事务,使用,默认就是使用RESOURCE_LOCAL事务。这两种事务的区别不在这里讨论。 **provider** EJB Persistence provider的一个实现类。如果不是使用多个厂商的 EJB Persistence实现,是不需要定义的。 **mapping-file** 指定映射文件的位置 **jar-file** 指定要解析的jar。jar中所有注解的类、包和所有的hbm.xml都会被添加到persistent-unit的配置中。主要用在JavaEE环境中。 **exclude-unlisted-classes** 不检查jar中加了@Entity注解的类。 **class** 明确指定要映射的类 **validation-mode** 实体的验证模式,默认是激活的。当一个实体在创建、更新,在实体发送到数据库前会被进行验证。CALLBACK: entities are validated on creation, update and deletion. If no Bean Validation provider is present, an exception is raised at initialization time. **properties** 配置厂商的一些特定属性。   下面这个包含了各种JPA的persistent.xml,用的是1.0规范,看的玩儿吧:  
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
    <persistence-unit name="eclipselink" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:hsqldb:mem:test"/>
            <property name="eclipselink.jdbc.user" value="sa"/>
            <property name="eclipselink.jdbc.password" value=""/>
            <property name="eclipselink.target-database"
                      value="org.eclipse.persistence.platform.database.HSQLPlatform"/>
            <property name="eclipselink.logging.level" value="OFF"/>
            <property name="eclipselink.orm.throw.exceptions" value="true"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="embeddedeclipselink" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:hsqldb:mem:test"/>
            <property name="eclipselink.jdbc.user" value="sa"/>
            <property name="eclipselink.jdbc.password" value=""/>
            <property name="eclipselink.target-database"
                      value="org.eclipse.persistence.platform.database.HSQLPlatform"/>
            <property name="eclipselink.logging.level" value="OFF"/>
            <property name="eclipselink.orm.throw.exceptions" value="true"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
            <property name="eclipselink.session.customizer"
                      value="org.compass.gps.device.jpa.embedded.eclipselink.CompassSessionCustomizer"/>
            <property name="compass.engine.connection" value="target/test-index"/>
            <property name="compass.debug" value="true" />
        </properties>
    </persistence-unit>

    <persistence-unit name="toplink" transaction-type="RESOURCE_LOCAL">
        <provider>oracle.toplink.essentials.PersistenceProvider</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="toplink.jdbc.url" value="jdbc:hsqldb:mem:test"/>
            <property name="toplink.jdbc.user" value="sa"/>
            <property name="toplink.jdbc.password" value=""/>
            <property name="toplink.target-database" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
            <property name="toplink.logging.level" value="OFF"/>
            <property name="toplink.orm.throw.exceptions" value="true"/>
            <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
            <property name="toplink.ddl-generation.output-mode" value="database"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="embeddedtoplink" transaction-type="RESOURCE_LOCAL">
        <provider>oracle.toplink.essentials.PersistenceProvider</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
            <property name="toplink.jdbc.url" value="jdbc:hsqldb:mem:test"/>
            <property name="toplink.jdbc.user" value="sa"/>
            <property name="toplink.jdbc.password" value=""/>
            <property name="toplink.target-database" value="oracle.toplink.essentials.platform.database.HSQLPlatform"/>
            <property name="toplink.logging.level" value="OFF"/>
            <property name="toplink.orm.throw.exceptions" value="true"/>
            <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
            <property name="toplink.ddl-generation.output-mode" value="database"/>
            <property name="toplink.session.customizer"
                      value="org.compass.gps.device.jpa.embedded.toplink.CompassSessionCustomizer"/>
            <property name="compass.engine.connection" value="target/test-index"/>
            <property name="compass.debug" value="true" />
        </properties>
    </persistence-unit>

    <persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.search.autoregister_listeners" value="false"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="embeddedhibernate" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:test"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.search.autoregister_listeners" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="compass.engine.connection" value="target/test-index"/>
            <property name="compass.debug" value="true" />
        </properties>
    </persistence-unit>

    <persistence-unit name="openjpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.DBDictionary" value="hsql"/>
            <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
            <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:mem:test"/>
            <property name="openjpa.ConnectionUserName" value="sa"/>
            <property name="openjpa.ConnectionPassword" value=""/>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="embeddedopenjpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>org.compass.gps.device.jpa.model.Simple</class>
        <class>org.compass.gps.device.jpa.model.SimpleBase</class>
        <class>org.compass.gps.device.jpa.model.SimpleExtend</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="openjpa.jdbc.DBDictionary" value="hsql"/>
            <property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
            <property name="openjpa.ConnectionURL" value="jdbc:hsqldb:mem:test"/>
            <property name="openjpa.ConnectionUserName" value="sa"/>
            <property name="openjpa.ConnectionPassword" value=""/>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
            <property name="compass.engine.connection" value="target/test-index"/>
            <property name="compass.debug" value="true" />
        </properties>
    </persistence-unit>
</persistence>
Jerky Lu wechat
欢迎加入微信公众号